// utils/eventBus.js  
class EventBus {
  constructor() {
    this.events  = {};
  }
 
  // 监听事件
  $on(eventName, callback) {
    this.events[eventName]  = this.events[eventName]  || [];
    this.events[eventName].push(callback); 
  }
 
  // 触发事件
  $emit(eventName, ...args) {
    if (this.events[eventName])  {
      this.events[eventName].forEach(callback  => {
        callback(...args);
      });
    }
  }
 
  // 移除事件监听
  $off(eventName, callback) {
    if (this.events[eventName])  {
      if (callback) {
        const index = this.events[eventName].indexOf(callback); 
        if (index > -1) {
          this.events[eventName].splice(index,  1);
        }
      } else {
        delete this.events[eventName]; 
      }
    }
  }
}
 
// 创建全局事件总线实例 
const eventBus = new EventBus();
 
export default eventBus;